import { Message } from 'element-ui'

let timer = null
let onceTimer = false

function alertMsg(message, alertType, duration) {
  if (message === '服务器超时' || message === '系统繁忙，请稍后再试') {
    return
  }
  if (message) {
    const options = {
      message: message,
      type: alertType,
      duration: duration || 2000,
      customClass: 'alert-z-index'
    }
    if (timer && alertType === 'success') {
      return
    }
    timer = Message(options)
    setTimeout(() => {
      clearTimeout(timer)
      timer = null
    }, options.duration)
  }
}

function alertMsgOnce(message, alertType, duration) {
  if (message) {
    const options = {
      message: message,
      type: alertType,
      duration: duration || 2000,
      customClass: 'alert-z-index'
    }
    if ((timer && alertType === 'success') || onceTimer) {
      return
    }
    onceTimer = true
    timer = Message(options)
    setTimeout(() => {
      clearTimeout(timer)
      timer = null
      onceTimer = false
    }, options.duration)
  }
}
export {
  alertMsg,
  alertMsgOnce
}
